package org.farng.mp3.id3;

import org.farng.mp3.InvalidTagException;

import java.io.IOException;
import java.io.RandomAccessFile;


/**
 * Title: Description: Copyright:    Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.6 $
 */
public class FrameBodyASPI extends AbstractID3v2FrameBody {
    /** DOCUMENT ME! */
    short[] fraction = null;

    /** DOCUMENT ME! */
    int bitsPerPoint = 0;

    /** DOCUMENT ME! */
    int dataLength = 0;

    /** DOCUMENT ME! */
    int dataStart = 0;

    /** DOCUMENT ME! */
    int indexPoints = 0;

    /**
     * Creates a new FrameBodyASPI object.
     */
    public FrameBodyASPI() {
        //        this.dataStart = 0;
        //        this.dataLength = 0;
        //        this.indexPoints = 0;
        //        this.bitsPerPoint = 0;
        //        this.fraction = new short[0];
    }

    public FrameBodyASPI(FrameBodyASPI copyObject) {
        super(copyObject);
        this.fraction     = (short[]) copyObject.fraction.clone();
        this.bitsPerPoint = copyObject.bitsPerPoint;
        this.dataLength   = copyObject.dataLength;
        this.dataStart    = copyObject.dataStart;
        this.indexPoints  = copyObject.indexPoints;
    }

    /**
     * Creates a new FrameBodyASPI object.
     *
     * @param dataStart DOCUMENT ME!
     * @param dataLength DOCUMENT ME!
     * @param indexPoints DOCUMENT ME!
     * @param bitsPerPoint DOCUMENT ME!
     * @param fraction DOCUMENT ME!
     */
    public FrameBodyASPI(int dataStart, int dataLength, int indexPoints, int bitsPerPoint, short[] fraction) {
        this.dataStart    = dataStart;
        this.dataLength   = dataLength;
        this.indexPoints  = indexPoints;
        this.bitsPerPoint = bitsPerPoint;
        this.fraction     = fraction;
    }

    /**
     * Creates a new FrameBodyASPI object.
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws InvalidTagException DOCUMENT ME!
     */
    public FrameBodyASPI(java.io.RandomAccessFile file)
                  throws IOException, InvalidTagException {
        this.read(file);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getIdentifier() {
        return "ASPI";
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return 4 + 4 + 2 + 1 + (this.fraction.length * 2);
    }

    /**
     * This method is not yet supported.
     *
     * @throws java.lang.UnsupportedOperationException This method is not yet
     *         supported
     */
    public void equals() {
        /**
         * @todo Implement this java.lang.Object method
         */
        throw new java.lang.UnsupportedOperationException("Method equals() not yet implemented.");
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws InvalidTagException DOCUMENT ME!
     */
    public void read(RandomAccessFile file)
              throws IOException, InvalidTagException {
        int size;

        size = readHeader(file);

        if (size == 0) {
            throw new InvalidTagException("Empty Frame");
        }

        this.dataStart    = file.readInt();
        this.dataLength   = file.readInt();
        this.indexPoints  = file.readShort();
        this.bitsPerPoint = file.readByte();

        fraction = new short[indexPoints];

        for (int i = 0; i < indexPoints; i++) {
            if (bitsPerPoint == 8) {
                fraction[i] = file.readByte();
            } else if (bitsPerPoint == 16) {
                fraction[i] = file.readShort();
            } else {
                throw new InvalidTagException("ASPI bits per point wasn't 8 or 16");
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return getIdentifier() + " " + this.dataStart + " " + this.dataLength + " " + this.indexPoints + " " + this.bitsPerPoint + " " + this.fraction.toString();
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     */
    public void write(RandomAccessFile file)
               throws IOException {
        writeHeader(file, this.getSize());

        file.writeInt(this.dataStart);
        file.writeInt(this.dataLength);
        file.writeShort(this.indexPoints);
        file.writeByte(16);

        for (int i = 0; i < indexPoints; i++) {
            file.writeShort(this.fraction[i]);
        }
    }
}